<?php

namespace Admin\Controller;

use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;

class MemberController extends Controller {

    //添加会员
    public function member_add(){

        if(IS_AJAX){

            $member = M('Member');
            $check_user = $member->where(array('member_tel' => I('member_tel')))->find();
            if ($check_user) {
                $this->error('登录手机号重复，请重新输入登录手机', 0, 0);
            }

            $memberRenum = I('member_renum');

            if(!empty($memberRenum)){

                if(!$member->where(array('member_mynum' => I('member_renum')))->find()){
                    $this->error('该推荐码无效，请重新输入', 0, 0);
                }

            }


            $numbered = date("Y-m-dH-i-s");
            $numbered = str_replace("-","",$numbered);
            $numbered .= rand(1000,9999);

            $sldata = array(
                'member_tel' => I('member_tel'),
                'member_pwd' => I('member_pwd', '', 'md5'),
                'member_realname' => I('member_realname'),
                'member_nickname' => I('member_nickname'),
                'member_idcard' => I('member_idcard'),
                'member_renum' => I('member_renum',''),
                'member_mynum' => $numbered,
                'member_state' => 0,
                'add_time' => time(),
                'display' => 0,
                'logins' => 0,
                'sort' => 0,
            );
            $member->add($sldata);

            $id = $member->add();

            $land = M('Land');

            $landData['land_member_id'] = $id;
            $landData['land_number'] = $numbered.$id;
            $landData['land_status'] = 0;
            $landData['land_type'] = 1;
            $landData['land_add_time'] = time();

            $land->add($landData);

            $this->success('会员添加成功', U('member_list'), 1);


        }else{
            $this->display();
        }

    }

    //会员列表
    public function member_list(){

        $member = M('Member');
        $val = I('val');

        $this->assign('testval', $val);
        if ($val) {
            $map['member_tel'] = array('like', "%" . $val . "%");
        }

        $count = $member->where($map)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数

        foreach ($map as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show(); // 分页显示输出

        $member_list = $member->where($map)->order('member_id')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('aid',cookie('aid'));
        $this->assign('member_list', $member_list);
        $this->assign('page', $show);

        $this->display();

    }

    //修改会员状态
    public function member_list_state(){

        $id = I('x');
        $status = M('Member')->where(array('member_id' => $id))->getField('member_state'); //判断当前状态情况
        if ($status == 1) {
            $statedata = array('member_state' => 0);
            $auth_group = M('Member')->where(array('member_id' => $id))->setField($statedata);
            $this->success('账号开启', 1, 1);
        } else {
            $statedata = array('member_state' => 1);
            $auth_group = M('Member')->where(array('member_id' => $id))->setField($statedata);
            $this->success('账号停用', 1, 1);
        }

    }

    //删除会员
    public function member_list_del(){

        $member_id = I('member_id');
        M('Member')->where(array('member_id' => I('member_id')))->delete();
        $this->redirect('member_list');

    }

    //修改会员信息
    public function member_list_edit(){

        if(IS_POST){

            $member = M('Member');

            $member_tel = I('member_tel_hidden');

            $data['member_tel'] = I('member_tel');
            $data['member_nickname'] = I('member_nickname');

            $data['member_id'] = I('member_id');

            if($member_tel != $data['member_tel']){

                if($member->where(array('member_tel'=> $data['member_tel']))->find()){
                    $this->error('登录手机号重复，请重新输入登录手机', 0, 0);
                }

            }

            $member->save($data);

            $this->success('会员修改成功', U('member_list'), 1);

        }else{

            $member_list = M('Member')->where(array('member_id' => I('member_id')))->find();
            $this->assign('member_list', $member_list);
            $this->display();

        }

    }

    //重置会员密码
    public function member_reset_pwd(){

        $member = M('Member');

        $data['member_id'] = I('member_id');
        $data['member_pwd'] = md5('123456');

        $member->save($data);

        $this->success('密码重置成功', U('member_list'), 1);

    }

    //会员登录日志
    public function member_login_log_list(){

        $loginLog = M('MemberLoginLog');
        $val = I('val');

        $this->assign('testval', $val);
        if ($val) {
            $map['member_tel'] = array('like', "%" . $val . "%");
        }

        $count = $loginLog->where($map)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数

        foreach ($map as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show(); // 分页显示输出

        $login_log_list = $loginLog->where($map)->order('login_id')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('login_log_list', $login_log_list);
        $this->assign('page', $show);

        $this->display();

    }

}
